﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBaoHanh.Template;
using MyCommon;
using QLBaoHanh.QLDanhMuc;
using QLBaoHanh.NghiepVu;
using QLBaoHanh.TiepNhan;
using QLBaoHanh.DoiBHSuaChua;
using QLBaoHanh.XuLyBaoHanhSuaChua;
using DataAccessLayer;
using QLBaoHanh.TraHang;
using QLBaoHanh.QuanTri;
using BussinessLayer;
using QLBaoHanh.BaoCao;

namespace QLBaoHanh
{
    public partial class frmMain : Form
    {
        public static NhanVien CurrentNhanVien;
        public static NguoiDung CurrentNguoiDung;
        private frmLogin _loginForm;
        public frmMain(frmLogin inLoginForm)
        {
            InitializeComponent();
            _loginForm = inLoginForm;
        }

        public void PhanQuyen(int VaiTroId)
        {
            if(CurrentNhanVien == null && CurrentNguoiDung.Username != "admin")
            {
                mn_nghiepvu.Visible = false;
                mn_category.Visible = false;
                mn_reports.Visible = false;
                mn_admin.Visible = false;
                MessageBox.Show("Tài khoản này chưa được gán cho nhân viên", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            switch(VaiTroId)
            {
                case (int)MyCommon.VaiTro.Admin:
                    //mn_nghiepvu.Visible = false;
                    //mn_reports.Visible = false;
                    break;
                case (int)MyCommon.VaiTro.NVKyThuat:
                    mn_reports.Visible = false;
                    mn_admin.Visible = false;
                    mn_receiving.Visible = false;
                    mn_doi_bhsc.Visible = false;
                    mn_do_immediatly.Visible = false;
                    mn_return_bhsc.Visible = false;
                    break;
                case (int)MyCommon.VaiTro.NVQuanLy:
                    //full quyen
                    break;
                case (int)MyCommon.VaiTro.NVTiepNhan:
                    mn_execute_bhsc.Visible = false;
                    mn_admin.Visible = false;
                    mn_reports.Visible = false;
                    break;
                default:
                    break;
            }
        }

        public void ExitApp()
        {
            this.Close();
            this.Dispose();
            _loginForm.SetUsernameTextbox(CurrentNguoiDung.Username, false);
            _loginForm.Show();
        }
        private void mn_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = (ToolStripMenuItem) sender;
            Form frmObj;
            if (menuItem == null)
            {
                return;
            }

            switch (menuItem.Name)
            {
                case Constants.MENU_SCREEN_LOCK:
                    //_loginForm.ShowDialog();
                    frmLogin LoginDialog = new frmLogin();
                    LoginDialog._parentForm = this;
                    LoginDialog.SetUsernameTextbox(CurrentNguoiDung.Username, true);
                    LoginDialog.SetIsLook(true);
                    LoginDialog.ShowDialog();
                    LoginDialog.FocusOnTxtPwd();
                    break;
                case Constants.MENU_CHANGE_PWD:
                    // Call form change pass
                    frmDoiMatKhau obj = new frmDoiMatKhau();
                    obj.ShowDialog();
                    break;
                case Constants.MENU_EXIT:
                    this.Close();
                    this.Dispose();
                    _loginForm.SetUsernameTextbox(CurrentNguoiDung.Username, false);
                    _loginForm.Show();
                    _loginForm.Focus();
                    break;
                case Constants.MENU_QUIT:
                    this.Close();
                    this.Dispose();
                    _loginForm.Close();
                    break;
                case Constants.MENU_CATE_CUSTOMERS :
                    frmObj = SingletonFormProvider.GetInstance<frmDsKhachHang>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_CATE_PROVIDERS:
                    frmObj = SingletonFormProvider.GetInstance<frmDsNhaCungCap>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_CATE_PRODUCES:
                    frmObj = SingletonFormProvider.GetInstance<frmDsNhaSanXuat>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_CATE_EMPLOYEES:
                    frmObj = SingletonFormProvider.GetInstance<frmDsNhanVien>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_CATE_SERVICES:
                    frmObj = SingletonFormProvider.GetInstance<frmDsLoaiDichVu>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_CATE_DEVICES:
                    //frmObj = SingletonFormProvider.GetInstance<frmDsThietBiKho>(this);
                    //frmObj.MdiParent = this;
                    //frmObj.Show();
                    //frmObj.Focus();
                    break;
                case Constants.MENU_DO_IMMEDIATLY: 
                    frmObj = SingletonFormProvider.GetInstance<frmDsPhienTheoTrangThai>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_RECEIVING:
                    frmObj = SingletonFormProvider.GetInstance<frmDsTiepNhan>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_DOI_BHSC:
                    frmObj = SingletonFormProvider.GetInstance<frmDsThietBiDoiBHSC>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_EXECUTE_BHSC:
                    frmObj = SingletonFormProvider.GetInstance<frmDsXuLyBHSC>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;

                case Constants.MENU_RETURN:
                    //frmObj = SingletonFormProvider.GetInstance<frmDsTraThietBiBHSC>(this);
                    frmObj = new frmDsTraThietBiBHSC();
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;
                case Constants.MENU_USER_MANAGEMENT:
                    frmObj = SingletonFormProvider.GetInstance<frmDsNguoiDung>(this);
                    //frmObj = new frmDsNguoiDung();
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    frmObj.Focus();
                    break;

                case Constants.MENU_GUIDE:
                    //Call huong dan su dung
                    break;
                case Constants.MENU_ABOUT:
                    //Call form about
                    frmObj = SingletonFormProvider.GetInstance<frmAbout>(this);
                    frmObj.ShowDialog();
                    break;
                //////////////////////////////////////////////////////
                //Menu bao cao
                case Constants.MENU_REPORT_DO_IMMEDIATELY:
                    //Call form report ds sua chua ngay
                    frmObj = SingletonFormProvider.GetInstance<frmBaoCaoSuaChuaNgay>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    break;
                case Constants.MENU_REPORT_RECEIVED:
                    //Call form report ds tiep nhan sua chua
                    frmObj = SingletonFormProvider.GetInstance<frmBaoCaoPhienTiepNhan>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    break;

                case Constants.MENU_REPORT_SAI_HAN:
                    //Call form report sua chua khong dung han
                    frmObj = SingletonFormProvider.GetInstance<frmBaoCaoPhienSaiHan>(this);
                    frmObj.MdiParent = this;
                    frmObj.Show();
                    break;
                    
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            PhanQuyen((int)CurrentNguoiDung.VaiTroId);
            CurrentNhanVien = NhanVienBO.getInstance.GetNhanVienByAccountId(CurrentNguoiDung.Id);
            if(CurrentNhanVien != null)
            {
                StatusNhanVien.Text = "Nhân viên: " + CurrentNhanVien.Ten + " | Vai trò: " + CurrentNguoiDung.VaiTro.Ten;
            }
            else
            {
                StatusNhanVien.Text = "Tài khoản này chưa được gán cho nhân viên";
            }
            
        }
    }
}
